<?php

namespace App\System\Base\Services;

use App\Exceptions\Customize\ParamsException;
use App\Exceptions\Customize\TestException;
use App\System\Base\Helpers\Traits\SinglePattern;
use App\System\Base\Services\Service;
use App\System\Cis\Model\CisUser;
use App\System\Cms\Model\CmsTopic;
use App\System\Cms\Model\CmsTopicAlbum;
use App\System\Cms\Model\CmsTopicAlbumEvaluation;
use App\System\Cms\Model\CmsTopicAlbumPraise;
use Illuminate\Support\Facades\DB;

class DeleteService extends Service
{
    use SinglePattern;

    /**
     * 删除数据
     * @param $type 1删除相册 2....
     * @param $ids 要删除的数组
     * @param $user_id 用户ID
     * @return boolean
     */
    public function delete($type, array $ids, $user_id = 0)
    {
        DB::beginTransaction();//开启事务
        switch ($type) {
            case 1:
                foreach ($ids as $v) {
                    $album_obj = CmsTopicAlbum::where('id', $v)->where('user_id', $user_id);

                    $album_info = $album_obj->first();
                    if ($album_info) {
                        $album_delete = $album_obj->delete();//删除相册
                        if ($album_delete) {
                            //1扣减话题图片数
                            $topic = CmsTopic::find($album_info->topic_id);
                            if ($topic) {
                                $topic->number = ($topic->number - 1) < 0 ? 0 : ($topic->number - 1);
                                $topic->image_number = ($topic->image_number - 1) < 0 ? 0 : ($topic->image_number - 1);
                                $topic->save();
                            }
                            //2删除话册点赞记录
                            CmsTopicAlbumPraise::where('topic_album_id', $album_info->id)->delete();
                            //3删除相册拼价记录
                            CmsTopicAlbumEvaluation::where('topic_album_id', $album_info->id)->delete();
                            //更新用户表相关统计数据
                            $user = CisUser::find($user_id);
                            if ($user) {
                                $user->praise_number = ($user->praise_number - $album_info->display_number) < 0 ? 0 : ($user->praise_number - $album_info->praise_number);
                                $user->victory_number = ($user->victory_number - $album_info->victory_number) < 0 ? 0 : ($user->victory_number - $album_info->victory_number);
                                $user->save();
                            }
                        } else {
                            DB::rollBack();//事务回滚
                            throw new ParamsException([
                                'status'  => 10000,
                                'message' => '删除失败'
                            ]);
                            break;
                        }
                    } else {
                        throw new ParamsException([
                            'status'  => 10000,
                            'message' => '数据不存在，删除失败！'
                        ]);
                        break;
                    }
                }
                DB::commit();//提交事务
                return apiResponse();
                break;

        }


    }
}